﻿import kliment.ApplicationAbstract;
import kliment.display.MCC;
import kliment.math.Calc;

/**
 * ...
 * @author	Kliment
 * @version	1.1.3
 */
class kliment.managers.CursorManager {
	static public var _instance:CursorManager;
	static private var _parent_mc:MovieClip;
	static private var _cursor_mc:MovieClip;
	static private var _subCursor_mc:MovieClip;
	
	static private var _cursor_rate:Number;
	static private var _sub_cursor_rate:Number;
	
	public function CursorManager() {
		if (!_instance) {
			if (ApplicationAbstract.instance) 
				_parent_mc = ApplicationAbstract.instance.layout_cursor_mc;
			else 
				_parent_mc = MCC.create(_root);
		}
	}
	
	static public function init():Void {
		if (!_instance) {
			_instance = new CursorManager();
		}
	}
	
	static public function setCursor(identifier:String, rate:Number):Void {
		init();
		removeCursor();
		_cursor_rate = _checkRate(rate);
		_cursor_mc = MCC.attach(identifier, _parent_mc, 'cursor');
		_moveToMouse(_cursor_mc);
		_cursor_mc.onEnterFrame = _moveCursor;
		Mouse.hide();
	}
	
	static public function removeCursor():Void {
		init();
		MCC.remove(_cursor_mc);
		Mouse.show();
	}
	
	static public function setSubCursor(identifier:String, rate:Number):Void {
		init();
		removeSubCursor();
		_sub_cursor_rate = _checkRate(rate);
		_subCursor_mc = MCC.attach(identifier, _parent_mc, 'sub_cursor');
		_moveToMouse(_subCursor_mc);
		_subCursor_mc.onEnterFrame = _moveSubCursor;
	}
	
	static public function removeSubCursor():Void {
		init();
		MCC.remove(_subCursor_mc);
	}
	
	static private function _moveToMouse(target_mc:MovieClip):Void {
		target_mc._x = _parent_mc._xmouse;
		target_mc._y = _parent_mc._ymouse;
	}
	
	static private function _moveCursor():Void {
		_move(_cursor_mc, _cursor_rate);
		Mouse.hide();
	}
	
	static private function _moveSubCursor():Void {
		_move(_subCursor_mc, _sub_cursor_rate);
	}
	
	static private function _move(target_mc:MovieClip, rate:Number):Void {
		target_mc._x += (_parent_mc._xmouse - target_mc._x) * rate;
		target_mc._y += (_parent_mc._ymouse - target_mc._y) * rate;
	}
	
	static private function _checkRate(value:Number):Number {
		if (value == undefined) 
			return 1;
		return Calc.inInterval(.1, 1, value);
	}
}